
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.3 LXML库的使用 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Xpath爬虫案例.html" />
    
    
    <link rel="prev" href="xpath与XML.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="正则表达式.html">
            
                <a href="正则表达式.html">
            
                    
                    第一章：正则表达式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="正则表达式入门.html">
            
                <a href="正则表达式入门.html">
            
                    
                    1.1 正则表达式入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Python中的正则表达式.html">
            
                <a href="Python中的正则表达式.html">
            
                    
                    1.2 Python中的正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="基本正则表达式.html">
            
                <a href="基本正则表达式.html">
            
                    
                    1.3 基本正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="分组.html">
            
                <a href="分组.html">
            
                    
                    1.4 分组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="先行断言.html">
            
                <a href="先行断言.html">
            
                    
                    1.5 先行断言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="标记.html">
            
                <a href="标记.html">
            
                    
                    1.6 标记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="替换与正则表达式编译.html">
            
                <a href="替换与正则表达式编译.html">
            
                    
                    1.7 替换与正则编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="正则表达式练习.html">
            
                <a href="正则表达式练习.html">
            
                    
                    1.8 正则表达式练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Python爬虫学习目标.html">
            
                <a href="Python爬虫学习目标.html">
            
                    
                    第二章：爬虫与Requests
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Requests的简单使用.html">
            
                <a href="Requests的简单使用.html">
            
                    
                    2.1 Requests的简单使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="深入了解Requests.html">
            
                <a href="深入了解Requests.html">
            
                    
                    2.2 深入了解Requets
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="结构化与非结构化.html">
            
                <a href="结构化与非结构化.html">
            
                    
                    第三章：非结构化与结构化数据提取
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="正则抓取.html">
            
                <a href="正则抓取.html">
            
                    
                    3.1 使用正则的爬虫
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="xpath与XML.html">
            
                <a href="xpath与XML.html">
            
                    
                    3.2 Xpath与XML类库
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.3" data-path="lxml.html">
            
                <a href="lxml.html">
            
                    
                    3.3 LXML库的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Xpath爬虫案例.html">
            
                <a href="Xpath爬虫案例.html">
            
                    
                    3.4 案例：贴吧图片
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Json与JsonPath.html">
            
                <a href="Json与JsonPath.html">
            
                    
                    3.5 Json与Json数据获取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="实例：糗事百科网站抓取.html">
            
                <a href="实例：糗事百科网站抓取.html">
            
                    
                    3.6 糗事百科网站爬虫
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.3 LXML库的使用</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="lxml&#x5E93;">lxml&#x5E93;</h2>
<p>lxml &#x662F; &#x4E00;&#x4E2A;HTML/XML&#x7684;&#x89E3;&#x6790;&#x5668;&#xFF0C;&#x4E3B;&#x8981;&#x7684;&#x529F;&#x80FD;&#x662F;&#x5982;&#x4F55;&#x89E3;&#x6790;&#x548C;&#x63D0;&#x53D6; HTML/XML &#x6570;&#x636E;&#x3002;</p>
<p>lxml&#x548C;&#x6B63;&#x5219;&#x4E00;&#x6837;&#xFF0C;&#x4E5F;&#x662F;&#x7528; C &#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x662F;&#x4E00;&#x6B3E;&#x9AD8;&#x6027;&#x80FD;&#x7684; Python HTML/XML &#x89E3;&#x6790;&#x5668;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x4E4B;&#x524D;&#x5B66;&#x4E60;&#x7684;XPath&#x8BED;&#x6CD5;&#xFF0C;&#x6765;&#x5FEB;&#x901F;&#x7684;&#x5B9A;&#x4F4D;&#x7279;&#x5B9A;&#x5143;&#x7D20;&#x4EE5;&#x53CA;&#x8282;&#x70B9;&#x4FE1;&#x606F;&#x3002;</p>
<p>lxml python &#x5B98;&#x65B9;&#x6587;&#x6863;&#xFF1A;<a href="http://lxml.de/index.html" target="_blank">http://lxml.de/index.html</a></p>
<p>&#x9700;&#x8981;&#x5B89;&#x88C5;C&#x8BED;&#x8A00;&#x5E93;&#xFF0C;&#x53EF;&#x4F7F;&#x7528; pip &#x5B89;&#x88C5;&#xFF1A;pip install lxml &#xFF08;&#x6216;&#x901A;&#x8FC7;wheel&#x65B9;&#x5F0F;&#x5B89;&#x88C5;&#xFF09;</p>
<h2 id="&#x521D;&#x6B65;&#x4F7F;&#x7528;">&#x521D;&#x6B65;&#x4F7F;&#x7528;</h2>
<p>&#x6211;&#x4EEC;&#x5229;&#x7528;&#x5B83;&#x6765;&#x89E3;&#x6790; HTML &#x4EE3;&#x7801;&#xFF0C;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4F7F;&#x7528; lxml &#x7684; etree &#x5E93;</span>
<span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

text = <span class="hljs-string">&apos;&apos;&apos;
&lt;div&gt;
    &lt;ul&gt;
         &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link1.html&quot;&gt;first item&lt;/a&gt;&lt;/li&gt;
         &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
         &lt;li class=&quot;item-inactive&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;third item&lt;/a&gt;&lt;/li&gt;
         &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
         &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt; # &#x6CE8;&#x610F;&#xFF0C;&#x6B64;&#x5904;&#x7F3A;&#x5C11;&#x4E00;&#x4E2A; &lt;/li&gt; &#x95ED;&#x5408;&#x6807;&#x7B7E;
     &lt;/ul&gt;
 &lt;/div&gt;
&apos;&apos;&apos;</span>

<span class="hljs-comment">#&#x5229;&#x7528;etree.HTML&#xFF0C;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x89E3;&#x6790;&#x4E3A;HTML&#x6587;&#x6863;</span>
html = etree.HTML(text)

<span class="hljs-comment"># &#x6309;&#x5B57;&#x7B26;&#x4E32;&#x5E8F;&#x5217;&#x5316;HTML&#x6587;&#x6863;</span>
result = etree.tostring(html)

print(result)
</code></pre>
<p>&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- hello.html --&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item-0&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;link1.html&quot;</span>&gt;</span>first item<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item-1&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;link2.html&quot;</span>&gt;</span>second item<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item-inactive&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;link3.html&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;bold&quot;</span>&gt;</span>third item<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item-1&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;link4.html&quot;</span>&gt;</span>fourth item<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item-0&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;link5.html&quot;</span>&gt;</span>fifth item<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>&#x518D;&#x5229;&#x7528; <code>etree.parse()</code> &#x65B9;&#x6CD5;&#x6765;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># lxml_parse.py</span>

<span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

<span class="hljs-comment"># &#x8BFB;&#x53D6;&#x5916;&#x90E8;&#x6587;&#x4EF6; hello.html</span>
html = etree.parse(<span class="hljs-string">&apos;./hello.html&apos;</span>)
result = etree.tostring(html, pretty_print=<span class="hljs-keyword">True</span>)

print(result)
</code></pre>
<p>&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E0E;&#x4E4B;&#x524D;&#x76F8;&#x540C;&#x3002;</p>
<h2 id="xpath&#x5B9E;&#x4F8B;&#x6D4B;&#x8BD5;">XPath&#x5B9E;&#x4F8B;&#x6D4B;&#x8BD5;</h2>
<h3 id="1-&#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;-li-&#x6807;&#x7B7E;">1. &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684; <code>&lt;li&gt;</code> &#x6807;&#x7B7E;</h3>
<pre><code class="lang-python"><span class="hljs-comment"># xpath_li.py</span>

<span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

html = etree.parse(<span class="hljs-string">&apos;hello.html&apos;</span>)
print(type(html))  <span class="hljs-comment"># &#x663E;&#x793A;etree.parse() &#x8FD4;&#x56DE;&#x7C7B;&#x578B;</span>

result = html.xpath(<span class="hljs-string">&apos;//li&apos;</span>)

print( result)  <span class="hljs-comment"># &#x6253;&#x5370;&lt;li&gt;&#x6807;&#x7B7E;&#x7684;&#x5143;&#x7D20;&#x96C6;&#x5408;</span>
<span class="hljs-keyword">print</span> (len(result))
<span class="hljs-keyword">print</span> (type(result))
<span class="hljs-keyword">print</span> (type(result[<span class="hljs-number">0</span>]))
</code></pre>
<p>&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>&lt;type &apos;lxml.etree._ElementTree&apos;&gt;
[&lt;Element li at 0x1014e0e18&gt;, &lt;Element li at 0x1014e0ef0&gt;, &lt;Element li at 0x1014e0f38&gt;, &lt;Element li at 0x1014e0f80&gt;, &lt;Element li at 0x1014e0fc8&gt;]
5
&lt;type &apos;list&apos;&gt;
&lt;type &apos;lxml.etree._Element&apos;&gt;
</code></pre><h3 id="2-&#x7EE7;&#x7EED;&#x83B7;&#x53D6;li-&#x6807;&#x7B7E;&#x7684;&#x6240;&#x6709;-class&#x5C5E;&#x6027;">2. &#x7EE7;&#x7EED;&#x83B7;&#x53D6;<code>&lt;li&gt;</code> &#x6807;&#x7B7E;&#x7684;&#x6240;&#x6709; <code>class</code>&#x5C5E;&#x6027;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

html = etree.parse(<span class="hljs-string">&apos;hello.html&apos;</span>)
result = html.xpath(<span class="hljs-string">&apos;//li/@class&apos;</span>)

print(result)
</code></pre>
<p>&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>[&apos;item-0&apos;, &apos;item-1&apos;, &apos;item-inactive&apos;, &apos;item-1&apos;, &apos;item-0&apos;]
</code></pre><h3 id="3-&#x7EE7;&#x7EED;&#x83B7;&#x53D6;li&#x6807;&#x7B7E;&#x4E0B;href-&#x4E3A;-link1html-&#x7684;-a-&#x6807;&#x7B7E;">3. &#x7EE7;&#x7EED;&#x83B7;&#x53D6;<code>&lt;li&gt;</code>&#x6807;&#x7B7E;&#x4E0B;<code>href</code> &#x4E3A; <code>link1.html</code> &#x7684; <code>&lt;a&gt;</code> &#x6807;&#x7B7E;</h3>
<pre><code>from lxml import etree

html = etree.parse(&apos;hello.html&apos;)
result = html.xpath(&apos;//li/a[@href=&quot;link1.html&quot;]&apos;)

print(result)
</code></pre><p>&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>[&lt;Element a at 0x10ffaae18&gt;]
</code></pre><h3 id="4&#x83B7;&#x53D6;li-&#x6807;&#x7B7E;&#x4E0B;&#x7684;&#x6240;&#x6709;-span-&#x6807;&#x7B7E;">4.&#x83B7;&#x53D6;<code>&lt;li&gt;</code> &#x6807;&#x7B7E;&#x4E0B;&#x7684;&#x6240;&#x6709; <code>&lt;span&gt;</code> &#x6807;&#x7B7E;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

html = etree.parse(<span class="hljs-string">&apos;hello.html&apos;</span>)
result = html.xpath(<span class="hljs-string">&apos;//li//span&apos;</span>)

print(result)
</code></pre>
<h3 id="5&#x83B7;&#x53D6;-li-&#x6807;&#x7B7E;&#x4E0B;&#x7684;a&#x6807;&#x7B7E;&#x91CC;&#x7684;&#x6240;&#x6709;-class">5.&#x83B7;&#x53D6; <code>&lt;li&gt;</code> &#x6807;&#x7B7E;&#x4E0B;&#x7684;<code>&lt;a&gt;</code>&#x6807;&#x7B7E;&#x91CC;&#x7684;&#x6240;&#x6709; <code>class</code></h3>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

html = etree.parse(<span class="hljs-string">&apos;hello.html&apos;</span>)
result = html.xpath(<span class="hljs-string">&apos;//li/a//@class&apos;</span>)

print(result)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</p>
<p><code>[&apos;blod&apos;]</code></p>
<h3 id="6&#x83B7;&#x53D6;&#x6700;&#x540E;&#x4E00;&#x4E2A;-li-&#x7684;-a-&#x7684;-href">6.&#x83B7;&#x53D6;&#x6700;&#x540E;&#x4E00;&#x4E2A; <code>&lt;li&gt;</code> &#x7684; <code>&lt;a&gt;</code> &#x7684; <code>href</code></h3>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

html = etree.parse(<span class="hljs-string">&apos;hello.html&apos;</span>)

result = html.xpath(<span class="hljs-string">&apos;//li[last()]/a/@href&apos;</span>)
<span class="hljs-comment"># &#x8C13;&#x8BED; [last()] &#x53EF;&#x4EE5;&#x627E;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;</span>

print(result)
</code></pre>
<p>&#x7ED3;&#x679C;&#xFF1A;</p>
<p><code>[&apos;link5.html&apos;]</code></p>
<h3 id="7&#x83B7;&#x53D6;&#x5012;&#x6570;&#x7B2C;&#x4E8C;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x5185;&#x5BB9;">7.&#x83B7;&#x53D6;&#x5012;&#x6570;&#x7B2C;&#x4E8C;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x5185;&#x5BB9;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

html = etree.parse(<span class="hljs-string">&apos;hello.html&apos;</span>)
result = html.xpath(<span class="hljs-string">&apos;//li[last()-1]/a&apos;</span>)

<span class="hljs-comment"># text &#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5143;&#x7D20;&#x5185;&#x5BB9;</span>
print(result[<span class="hljs-number">0</span>].text)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</p>
<p><code>fourth item</code></p>
<h3 id="8&#x83B7;&#x53D6;-class-&#x503C;&#x4E3A;-bold-&#x7684;&#x6807;&#x7B7E;&#x540D;">8.&#x83B7;&#x53D6; <code>class</code> &#x503C;&#x4E3A; <code>bold</code> &#x7684;&#x6807;&#x7B7E;&#x540D;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree

html = etree.parse(<span class="hljs-string">&apos;hello.html&apos;</span>)

result = html.xpath(<span class="hljs-string">&apos;//*[@class=&quot;bold&quot;]&apos;</span>)

<span class="hljs-comment"># tag&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x6807;&#x7B7E;&#x540D;</span>
print(result[<span class="hljs-number">0</span>].tag)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</p>
<p><code>span</code></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="xpath与XML.html" class="navigation navigation-prev " aria-label="Previous page: 3.2 Xpath与XML类库">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Xpath爬虫案例.html" class="navigation navigation-next " aria-label="Next page: 3.4 案例：贴吧图片">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.3 LXML库的使用","level":"1.4.3","depth":2,"next":{"title":"3.4 案例：贴吧图片","level":"1.4.4","depth":2,"path":"notes/Xpath爬虫案例.md","ref":"notes/Xpath爬虫案例.md","articles":[]},"previous":{"title":"3.2 Xpath与XML类库","level":"1.4.2","depth":2,"path":"notes/xpath与XML.md","ref":"notes/xpath与XML.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"notes/lxml.md","mtime":"2019-05-04T08:39:48.019Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-05-08T02:50:45.307Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

